﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Controls.HeaderGridView;

namespace YJKArch.DoorWinModule
{
	public partial class DlgDoorWinStatistics : System.Windows.Forms.Form
	{
		public static DlgDoorWinStatistics GetInstance(UIApplication app)
		{
			if (DlgDoorWinStatistics.ddws == null || DlgDoorWinStatistics.ddws.IsDisposed)
			{
				DlgDoorWinStatistics.ddws = new DlgDoorWinStatistics(app);
			}
			return DlgDoorWinStatistics.ddws;
		}

		public DlgDoorWinStatistics(UIApplication app)
		{
			this.m_application = app;
			this.m_document = app.ActiveUIDocument.Document;
			this.InitializeComponent();
			HeaderUtils.LoadXml(AssistFunc.GetAppRootPath() + "\\Data\\默认门窗表.xml", this.headerTree);
			foreach (object obj in this.headerTree.Nodes)
			{
				TreeNode treeNode = (TreeNode)obj;
				this.gridView.HeadSource.Add((TreeNode)treeNode.Clone());
			}
			this.comboBox1.SelectedIndex = 0;
		}

		private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (this.comboBox1.SelectedIndex == 0)
			{
				this.comboBox2.Items.Clear();
				Level currentViewLevel = AssistFunc.GetCurrentViewLevel(this.m_document);
				this.comboBox2.Items.Add(currentViewLevel.Name);
				if (this.comboBox2.Items.Count > 0)
				{
					this.comboBox2.SelectedIndex = 0;
				}
				DoorWinStatistics doorWinStatistics = new DoorWinStatistics();
				doorWinStatistics.StatisticsByLevel(this.m_document, currentViewLevel);
				this.gridView.DataSource = HeaderUtils.ToDataTable<DoorWinStatisticsResult>(doorWinStatistics.arResults, HeaderUtils.GetChilds(this.gridView.HeadSource), DoorWinStatisticsResult.dict);
				return;
			}
			if (this.comboBox1.SelectedIndex == 1)
			{
				this.comboBox2.Items.Clear();
				this.comboBox2.Items.Add("所有层");
				foreach (Level level in AssistFunc.GetAllLevel(this.m_document))
				{
					this.comboBox2.Items.Add(level.Name);
				}
				if (this.comboBox2.Items.Count > 0)
				{
					this.comboBox2.SelectedIndex = 0;
				}
				DoorWinStatistics doorWinStatistics2 = new DoorWinStatistics();
				doorWinStatistics2.StatisticsByDoc(this.m_document);
				this.gridView.DataSource = HeaderUtils.ToDataTable<DoorWinStatisticsResult>(doorWinStatistics2.arResults, HeaderUtils.GetChilds(this.gridView.HeadSource), DoorWinStatisticsResult.dict);
			}
		}

		private void button2_Click_1(object sender, EventArgs e)
		{
			this.gridView.ExportExcel();
		}

		private void comboBox2_SelectedIndexChanged_1(object sender, EventArgs e)
		{
			if (this.comboBox2.SelectedItem != null)
			{
				Level levelByName = AssistFunc.GetLevelByName(this.m_document, this.comboBox2.SelectedItem.ToString().Trim());
				if (levelByName != null)
				{
					DoorWinStatistics doorWinStatistics = new DoorWinStatistics();
					doorWinStatistics.StatisticsByLevel(this.m_document, levelByName);
					this.gridView.DataSource = HeaderUtils.ToDataTable<DoorWinStatisticsResult>(doorWinStatistics.arResults, HeaderUtils.GetChilds(this.gridView.HeadSource), DoorWinStatisticsResult.dict);
					return;
				}
				DoorWinStatistics doorWinStatistics2 = new DoorWinStatistics();
				doorWinStatistics2.StatisticsByDoc(this.m_document);
				this.gridView.DataSource = HeaderUtils.ToDataTable<DoorWinStatisticsResult>(doorWinStatistics2.arResults, HeaderUtils.GetChilds(this.gridView.HeadSource), DoorWinStatisticsResult.dict);
			}
		}

		private void button1_Click(object sender, EventArgs e)
		{
			using (DlgHeaderTree dlgHeaderTree = new DlgHeaderTree(DoorWinStatisticsResult.dict, this.gridView, AssistFunc.GetAppRootPath() + "\\Data\\默认门窗表.xml"))
			{
				string str = AssistFunc.GetAppRootPath() + "\\Data";
				HeaderFile item = new HeaderFile(new FileInfo(str + "\\默认门窗表.xml"));
				HeaderFile item2 = new HeaderFile(new FileInfo(str + "\\初始门窗表.xml"));
				HeaderFile item3 = new HeaderFile(new FileInfo(str + "\\传统门窗表.xml"));
				HeaderFile item4 = new HeaderFile(new FileInfo(str + "\\标准门窗表.xml"));
				HeaderFile item5 = new HeaderFile(new FileInfo(str + "\\复合表头门窗表.xml"));
				dlgHeaderTree.cbxDefault.Items.Add(item);
				dlgHeaderTree.cbxDefault.Items.Add(item2);
				dlgHeaderTree.cbxDefault.Items.Add(item3);
				dlgHeaderTree.cbxDefault.Items.Add(item4);
				dlgHeaderTree.cbxDefault.Items.Add(item5);
				dlgHeaderTree.cbxDefault.SelectedIndex = 0;
				if (dlgHeaderTree.ShowDialog() == DialogResult.OK)
				{
					Level levelByName = AssistFunc.GetLevelByName(this.m_document, this.comboBox2.SelectedItem.ToString().Trim());
					if (levelByName != null)
					{
						DoorWinStatistics doorWinStatistics = new DoorWinStatistics();
						doorWinStatistics.StatisticsByLevel(this.m_document, levelByName);
						this.gridView.DataSource = HeaderUtils.ToDataTable<DoorWinStatisticsResult>(doorWinStatistics.arResults, HeaderUtils.GetChilds(this.gridView.HeadSource), DoorWinStatisticsResult.dict);
					}
					else
					{
						DoorWinStatistics doorWinStatistics2 = new DoorWinStatistics();
						doorWinStatistics2.StatisticsByDoc(this.m_document);
						this.gridView.DataSource = HeaderUtils.ToDataTable<DoorWinStatisticsResult>(doorWinStatistics2.arResults, HeaderUtils.GetChilds(this.gridView.HeadSource), DoorWinStatisticsResult.dict);
					}
				}
			}
		}

		private void button3_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private void DlgDoorWinStatistics_FormClosed(object sender, FormClosedEventArgs e)
		{
		}

		private void splitContainer1_Panel1_Paint(object sender, PaintEventArgs e)
		{
		}

		private void splitContainer1_SplitterMoved(object sender, SplitterEventArgs e)
		{
		}

		public TreeView headerTree = new TreeView();

		private UIApplication m_application;

		private Autodesk.Revit.DB.Document m_document;

		private static DlgDoorWinStatistics ddws;
	}
}
